/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.tukano.swingdeco.containers;

import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.Scrollable;

/**
 * A scrollable panel that uses a DynamicGridLayout
 * @author pgi
 */
public class DynamicGridPanel extends JPanelExt implements Scrollable {

    private static final long serialVersionUID = 0;

    private int scrollableBlock = 25;
    private int scrollableUnit = 5;

    /**
     * Initializes this panel with a margin of 0 points
     */
    public DynamicGridPanel() {
        this(0);
    }

    /**
     * Initializes this panel using the given inter-component margin
     * @param margin the space between a component and the adjacent one
     */
    public DynamicGridPanel(int margin) {
        super(new DynamicGridLayout(margin));
    }

    /**
     * Returns the scrollable block value
     * @return the scrollable block value
     */
    public int getScrollableBlock() {
        return scrollableBlock;
    }

    public void setScrollableBlock(int scrollableBlock) {
        this.scrollableBlock = scrollableBlock;
    }

    public int getScrollableUnit() {
        return scrollableUnit;
    }

    public void setScrollableUnit(int scrollableUnit) {
        this.scrollableUnit = scrollableUnit;
    }

    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return scrollableUnit;
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return scrollableBlock;
    }

    public boolean getScrollableTracksViewportWidth() {
        return true;
    }

    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

    public DynamicGridPanel withTitledBorder(String string) {
        setBorder(BorderFactory.createTitledBorder(string));
        return this;
    }

    public void clear() {
        removeAll();
        revalidate();
        repaint();
    }
}
